<?php

function EncryptedPin($sPin, $sCardNo, $sPubKeyURL) {
	$sPubKeyURL = trim(SDK_ENCRYPT_CERT_PATH, " ");
	$fp = fopen($sPubKeyURL, "r");
	if ($fp != NULL) {
		$sCrt = fread($fp, 8192);
		fclose($fp);
	}
	$sPubCrt = openssl_x509_read($sCrt);
	if ($sPubCrt === FALSE) {
		print("openssl_x509_read in false!");
		return (-1);
	}
	$sPubKey = openssl_x509_parse($sPubCrt);

	$sInput = Pin2PinBlockWithCardNO($sPin, $sCardNo);
	if ($sInput == 1) {
		print("Pin2PinBlockWithCardNO Error ! : " . $sInput);
		return (1);
	}
	$iRet = openssl_public_encrypt($sInput, $sOutData, $sCrt, OPENSSL_PKCS1_PADDING);
	if ($iRet === TRUE) {
		$sBase64EncodeOutData = base64_encode($sOutData);
		return $sBase64EncodeOutData;
	} else {
		print("openssl_public_encrypt Error !");
		return (-1);
	}
}

function coverParamsToString($params) {
	$sign_str = '';
	ksort($params);
	foreach ($params as $key => $val) {
		if ($key == 'signature') {
			continue;
		}
		$sign_str .= sprintf("%s=%s&", $key, $val);
	}
	return substr($sign_str, 0, strlen($sign_str) - 1);
}

function coverStringToArray($str) {
	$result = array();

	if (!empty($str)) {
		$temp = preg_split('/&/', $str);
		if (!empty($temp)) {
			foreach ($temp as $key => $val) {
				$arr = preg_split('/=/', $val, 2);
				if (!empty($arr)) {
					$k = $arr['0'];
					$v = $arr['1'];
					$result[$k] = $v;
				}
			}
		}
	}
	return $result;
}

function deal_params(&$params) {

	if (!empty($params['customerInfo'])) {
		$params['customerInfo'] = base64_decode($params['customerInfo']);
	}

	if (!empty($params['encoding']) && strtoupper($params['encoding']) == 'utf-8') {
		foreach ($params as $key => $val) {
			$params[$key] = iconv('utf-8', 'UTF-8', $val);
		}
	}
}

function deflate_file(&$params) {
	foreach ($_FILES as $file) {
		if (file_exists($file['tmp_name'])) {
			$params['fileName'] = $file['name'];

			$file_content = file_get_contents($file['tmp_name']);
			$file_content_deflate = gzcompress($file_content);

			$params['fileContent'] = base64_encode($file_content_deflate);
		}
	}
}

function deal_file($params) {
	if (isset($params['fileContent'])) {
		$fileContent = $params['fileContent'];

		if (empty($fileContent)) {
		} else {
			$content = gzuncompress(base64_decode($fileContent));
			$root = SDK_FILE_DOWN_PATH;
			$filePath = null;
			if (empty($params['fileName'])) {
				$filePath = $root . $params['merId'] . '_' . $params['batchNo'] . '_' . $params['txnTime'] . 'txt';
			} else {
				$filePath = $root . $params['fileName'];
			}
			$handle = fopen($filePath, "w+");
			if (!is_writable($filePath)) {
			} else {
				file_put_contents($filePath, $content);
			}
			fclose($handle);
		}
	}
}

function create_html($params, $action) {
	$encodeType = isset($params['encoding']) ? $params['encoding'] : 'UTF-8';
	$html = <<<eot
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
</head>
<body  onload="javascript:document.pay_form.submit();">
    <form id="pay_form" name="pay_form" action="{$action}" method="post">
	
eot;
	foreach ($params as $key => $value) {
		$html .= "    <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
	}
	$html .= <<<eot
    <input type="submit" type="hidden" value="稍等，支付跳转跳..." style="border:none;">
    </form>
</body>
</html>
eot;
	return $html;
}

function Pin2PinBlock(&$sPin) {
	$iTemp = 1;
	$sPinLen = strlen($sPin);
	$sBuf = array();
	$sBuf[0] = intval($sPinLen, 10);

	if ($sPinLen % 2 == 0) {
		for ($i = 0; $i < $sPinLen; ) {
			$tBuf = substr($sPin, $i, 2);
			$sBuf[$iTemp] = intval($tBuf, 16);
			unset($tBuf);
			if ($i == ($sPinLen - 2)) {
				if ($iTemp < 7) {
					$t = 0;
					for ($t = ($iTemp + 1); $t < 8; $t++) {
						$sBuf[$t] = 0xff;
					}
				}
			}
			$iTemp++;
			$i = $i + 2;
		}
	} else {
		for ($i = 0; $i < $sPinLen; ) {
			if ($i == ($sPinLen - 1)) {
				$mBuf = substr($sPin, $i, 1) . "f";
				$sBuf[$iTemp] = intval($mBuf, 16);
				unset($mBuf);
				if (($iTemp) < 7) {
					$t = 0;
					for ($t = ($iTemp + 1); $t < 8; $t++) {
						$sBuf[$t] = 0xff;
					}
				}
			} else {
				$tBuf = substr($sPin, $i, 2);
				$sBuf[$iTemp] = intval($tBuf, 16);
				unset($tBuf);
			}
			$iTemp++;
			$i = $i + 2;
		}
	}
	return $sBuf;
}

function FormatPan(&$sPan) {
	$iPanLen = strlen($sPan);
	$iTemp = $iPanLen - 13;
	$sBuf = array();
	$sBuf[0] = 0x00;
	$sBuf[1] = 0x00;
	for ($i = 2; $i < 8; $i++) {
		$tBuf = substr($sPan, $iTemp, 2);
		$sBuf[$i] = intval($tBuf, 16);
		$iTemp = $iTemp + 2;
	}
	return $sBuf;
}

function Pin2PinBlockWithCardNO(&$sPin, &$sCardNO) {
	$sPinBuf = Pin2PinBlock($sPin);
	$iCardLen = strlen($sCardNO);
	if ($iCardLen <= 10) {
		return (1);
	} elseif ($iCardLen == 11) {
		$sCardNO = "00" . $sCardNO;
	} elseif ($iCardLen == 12) {
		$sCardNO = "0" . $sCardNO;
	}
	$sPanBuf = FormatPan($sCardNO);
	$sBuf = array();

	for ($i = 0; $i < 8; $i++) {
		$sBuf[$i] = vsprintf("%c", ($sPinBuf[$i] ^ $sPanBuf[$i]));
	}
	unset($sPinBuf);
	unset($sPanBuf);
	$sOutput = implode("", $sBuf);
	return $sOutput;
}
?>